<?php
/**
 * 
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Home\Controller;
use Think\Controller;

class EqmentController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	public function network(){
		if(IS_GET){
			$network_id = I('network_id') ? (int)I('network_id') : 0;
			if($network_id){
				$info = D('Network')->getNetworkInfoById($network_id);
				if(is_array($info) && count($info)){
					$result = array('errCode' => 1, 'info' => $info);
				}else{
					$result = array('errCode' => 0);
				}
				$this->ajaxReturn($result, 'json');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Network')->getNetworkListCount();
				//用户列表
				$list = D('Network')->getNetworkList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('network');
				}else{
					$this->display('network_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'network_title' => I('post.network_title') ? trim(I('post.network_title')) : '',
				'network_nums' => I('post.network_nums') ? trim(I('post.network_nums')) : '',
				'network_barcode' => I('post.network_barcode') ? trim(I('post.network_barcode')) : '',
				'channel' => I('post.channel') ? (int)I('post.channel') : 0
			);
			
			$network_id = I('post.network_id') ? (int)I('post.network_id') : 0;
			if(empty($data['network_title']) || empty($data['network_nums'])){
				$this->ajaxReturn('请输入网管名称或网管编号');
			}else{
				if($network_id){
					$row = D('Network')->editMemberInfo($network_id, $data);
				}else{
					$info = D('Network')->getNetworkInfoByNetworkNums($data['network_nums']);
					if(is_array($info) && count($info)){
						$this->ajaxReturn('网管编号已存在');
						exit;
					}else{
						$row = D('Network')->addNetworkInfo($data);
					}
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	/**
	 * 门锁分配
	 **/
	public function lock(){
		if(IS_GET){
			$lock_id = I('lock_id') ? (int)I('lock_id') : 0;
			if($lock_id){
				$info = D('Lock')->getLockInfoByLockId($lock_id);
				if(is_array($info) && count($info)){
					//获取对应的网管编号
					$network = D('Network')->getNetworkInfoById($info['network_id']);
					if(is_array($network) && count($network)){
						$info['network_nums'] = $network['network_nums'];
					}
					//获取对应的房间号
					$room = D('Room')->getRoomInfoById($info['room_id']);
					if(is_array($room) && count($room)){
						$info['room_number'] = $room['room_number'];
					}
					
					$result = array('errCode' => 1, 'info' => $info);
				}else{
					$result = array('errCode' => 0);
				}
				$this->ajaxReturn($result, 'json');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Lock')->getLockListCount();
				//用户列表
				$list = D('Lock')->getLockList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('lock');
				}else{
					$this->display('lock_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'cmd_id' => I('post.cmd_id')
			);
			
			if($data['cmd_id'] < 0){
				$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '请输入门锁ID'), 'json');
				exit;
			}
			
			$network_nums = I('post.network_nums');
			//获取网管ID
			$network = D('Network')->getNetworkInfoByNetworkNums($network_nums);
			if(is_array($network) && count($network)){
				$data['network_id'] = $network['network_id'];
			}else{
				$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '网管不存在'), 'json');
				exit;
			}
			
			$room_number = I('post.room_number') ? trim(I('post.room_number')) : '';
			$room = D('Room')->getRoomInfoByNumber($room_number);
			if(is_array($room) && count($room)){
				$data['room_id'] = $room['room_id'];
			}else{
				$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '房间不存在'), 'json');
				exit;
			}
			//获取门锁与网管配置的房间信息
			$res = D('Lock')->getLockInfoByLockIdAndNetWorkID($data['cmd_id'], $network['network_id']);
			
			$lock_id = I('post.lock_id') ? (int)I('post.lock_id') : 0;
			
			if($lock_id){ //编辑
				$lockinfo = D('Lock')->getLockInfoByLockId($lock_id);
				if(is_array($res) && $res['room_id'] != $room['room_id']){
					$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '该门锁已分配到其他房间'), 'json');
				}else if($lockinfo['room_id'] != $room['room_id'] && D('Lock')->getLockInfoByRoomNumber($room['room_id'])){
					$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '该房间已分配门锁'), 'json');
				}else{
					$row = D('Lock')->editLockAndNetworkInfo($lock_id, $data);
					$log = array(
						'room_id' => $room['room_id'],
						'room_number' => $room['room_number'],
						'network_id' => $network['network_id'],
						'network_nums' => $network['network_nums'],
						'lock_id' => $data['cmd_id'],
						'log_time' => time(),
						'user_id' => session('user_id'),
						'visible' => 2
					);
				}
			}else{ //添加
				if(is_array($res) && count($res)){
					$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '该门锁已分配到其他房间'), 'json');
				}else if(D('Lock')->getLockInfoByRoomNumber($room['room_id'])){
					$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '该房间已分配门锁'), 'json');
				}else{
					$row = D('Lock')->addLockAndNetworkAndRoom($data);
					$log = array(
						'room_id' => $room['room_id'],
						'room_number' => $room['room_number'],
						'network_id' => $network['network_id'],
						'network_nums' => $network['network_nums'],
						'lock_id' => $data['cmd_id'],
						'log_time' => time(),
						'user_id' => session('user_id'),
						'visible' => 1
					);
				}
			}
			
			if($row !== false){
				//添加日志
				D('Locklog')->addLockLogInfo($log);
				$this->ajaxReturn(array('errCode' => 1, 'errMsg' => '操作成功'), 'json');
			}else{
				$this->ajaxReturn(array('errCode' => 0, 'errMsg' => '操作失败'), 'json');
			}
		}
	}
	
	/**
	 * 房卡管理
	 **/
	public function card(){
		if(IS_GET){
			$card_id = I('card_id') ? (int)I('card_id') : 0;
			if($card_id){
				$info = D('Roomcard')->getRoomcardInfoById($card_id);
				if(is_array($info) && count($info)){
					$result = array('errCode' => 1, 'info' => $info);
				}else{
					$result = array('errCode' => 0);
				}
				$this->ajaxReturn($result, 'json');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Roomcard')->getRoomcardListCount();
				//用户列表
				$list = D('Roomcard')->getRoomcardList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('card');
				}else{
					$this->display('card_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'card' => I('post.card') ? trim(I('post.card')) : '',
				'card_udid' => I('post.card_udid') ? trim(I('post.card_udid')) : ''
			);
			
			$card_id = I('post.card_id') ? (int)I('post.card_id') : 0;
			if(empty($data['card']) || empty($data['card_udid'])){
				$this->ajaxReturn('请输入卡号或房卡UDID');
			}else{
				if($card_id){
					$row = D('Roomcard')->editRoomcardInfo($card_id, $data);
				}else{
					$info = D('Roomcard')->getRoomCardInfoByCard($data['card']);
					if(is_array($info) && count($info)){
						$this->ajaxReturn('卡号已存在');
						exit;
					}else{
						$row = D('Roomcard')->addRoomcardInfo($data);
					}
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	/**
	 * 员工管理
	 *
	 **/
	public function staff(){
		if(IS_GET){
			$staff_id = I('staff_id') ? (int)I('staff_id') : 0;
			if($staff_id){
				$info = D('Staff')->getStaffInfoById($staff_id);
				if(is_array($info) && count($info)){
					$result = array('errCode' => 1, 'info' => $info);
				}else{
					$result = array('errCode' => 0);
				}
				$this->ajaxReturn($result, 'json');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Staff')->getStaffListCount();
				//用户列表
				$list = D('Staff')->getStaffList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('staff');
				}else{
					$this->display('staff_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'staff_name' => I('post.staff_name') ? trim(I('post.staff_name')) : '',
				'card_udid' => I('post.card_udid') ? trim(I('post.card_udid')) : ''
			);
			$staff_id = I('post.staff_id') ? (int)I('post.staff_id') : 0;
			if(empty($data['staff_name']) || empty($data['card_udid'])){
				$this->ajaxReturn('请输入姓名或身份证UDID');
			}else{
				if($staff_id){
					$row = D('Staff')->editStaffInfo($staff_id, $data);
				}else{
					$row = D('Staff')->addStaffInfo($data);
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	/**
	 * 删除
	 **/
	public function remove(){
		if(IS_POST){
			$id = I('post.id') ? (int)I('post.id') : 0;
			$type = I('post.type') ? trim(I('post.type')) : '';
			
			switch($type){
				case 'network':
					$row = D('Network')->delNetworkInfo($id);
				break;
				case 'lock':
					$row = D('Lock')->delLockAndNetworkById($id);
				break;
				case 'staff':
					$row = D('Staff')->delStaffInfo($id);
				break;
				case 'card':
					$row = D('Roomcard')->delRoomcardInfo($id);
				break;
				default:
				break;
			}
			
			if($row !== false){
				$this->ajaxReturn(1, 'json');
			}else{
				$this->ajaxReturn(0, 'json');
			}
		}
	}
}


   